package org.wdcode.core.datasource;

import org.wdcode.common.exception.CustomException;
import org.wdcode.core.factory.BaseFactory;
import org.wdcode.core.params.WdCoreParams;

/**
 * 获得数据源 DataSource 的工厂类
 * @author WD
 * @since JDK6
 * @version 1.0 2009-08-13
 */
public final class DataSourceFactory extends BaseFactory<DataSource> {
	// 工厂
	private final static DataSourceFactory	FACTORY;

	// 静态初始化
	static {
		FACTORY = new DataSourceFactory();
	}

	/**
	 * 私有构造
	 */
	private DataSourceFactory() {
		super();
	}

	/**
	 * 返回工厂
	 * @return 工厂
	 */
	public final static DataSourceFactory getFactory() {
		return FACTORY;
	}

	/**
	 * 实例一个新的数据源
	 * @return DataSource
	 * @throws CustomException 获取数据源失败
	 */
	@Override
	public final DataSource newInstance() {
		// 声明数据源
		DataSource ds = new BasicDataSource(WdCoreParams.getDataSourceParse());
		// 设置属性
		ds.setDriver(WdCoreParams.getDataSourceDriver());
		ds.setUrl(WdCoreParams.getDataSourceUrl());
		ds.setUser(WdCoreParams.getDataSourceUser());
		ds.setPassword(WdCoreParams.getDataSourcePassword());
		ds.setMaxPoolSize(WdCoreParams.getDataSourceMaxPoolSize());
		ds.setMinPoolSize(WdCoreParams.getDataSourceMinPoolSize());
		ds.setMaxSize(WdCoreParams.getDataSourceMaxSize());
		ds.setTimeout(WdCoreParams.getDataSourceTimeout());
		ds.setIdleTimeout(WdCoreParams.getDataSourceIdleTimeout());
		ds.setInitialPoolSize(WdCoreParams.getDataSourceInitialPoolSize());
		ds.setMaxIdleTime(WdCoreParams.getDataSourceMaxIdleTime());
		// 返回数据源
		return ds;
	}
}
